package com.webmagic.webmagic.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.webmagic.webmagic.config.MybatisConfig;
import com.webmagic.webmagic.config.MysqlConfig;

import java.io.IOException;
import java.sql.SQLException;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author xbl
 * @Description
 * @date 2018/12/24
 */
@Configuration
public class DaoConfiguration {

    @Resource
    private MysqlConfig mysqlConfig;
    @Resource
    private MybatisConfig mybatisConfig;
    
    @Bean
    public ServletRegistrationBean DruidStatViewServlet(){
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //设置白名单
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //设置黑名单
        servletRegistrationBean.addInitParameter("deny","192.168.1.73");
        //登录查看信息的账号密码.
        servletRegistrationBean.addInitParameter("loginUsername","admin");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean druidStatFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

    @Bean
    public DataSource DataSource() {
        DruidDataSource data = new DruidDataSource();
        data.setUrl(mysqlConfig.getUrl());
        data.setUsername(mysqlConfig.getUsername());
        data.setPassword(mysqlConfig.getPassword());
        data.setInitialSize(mysqlConfig.getInitialSize());
        data.setMinIdle(mysqlConfig.getMinIdle());
        data.setMaxActive(mysqlConfig.getMaxActive());
        data.setMaxWait(mysqlConfig.getMaxWait());
        data.setTimeBetweenEvictionRunsMillis(mysqlConfig.getTimeBetweenEvictionRunsMillis());
        data.setMinEvictableIdleTimeMillis(mysqlConfig.getMinEvictableIdleTimeMillis());
        data.setValidationQuery(mysqlConfig.getValidationQuery());
        data.setTestWhileIdle(mysqlConfig.getTestWhileIdle());
        data.setTestOnBorrow(mysqlConfig.getTestOnBorrow());
        data.setTestOnReturn(mysqlConfig.getTestOnReturn());
        try {
            data.setFilters(mysqlConfig.getFilters());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return data;
    }

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean creatSqlSessionFactoryBean(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setTypeAliasesPackage(mybatisConfig.getSettypealiasespackage());
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        //sessionFactoryBean.setMapperLocations(resolver.getResources(mybatisConfig.getMapperLocations()));
        return sessionFactoryBean;
    }

}
